﻿using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxEditors;
using Kaysite.Controller;
using Kaysite.Manager;
using Kaysite.Model;

public partial class Controls_AdminControls_UserEdit : BaseEditControl<UserManager, UserController, User>
{
    protected override ASPxCallback GetCallbackControl()
    {
        return clbkSave;
    }

    protected override ASPxButton GetCancelButton()
    {
        return btnCancel;
    }

    protected override ASPxButton GetOkButton()
    {
        return btnOk;
    }

    protected override void InitControlsByModelData(User model)
    {
        if (model == null)
            return;
        txtDisplayName.Text = model.Name;
        txtUserName.Text = model.UserName;
        txtEmail.Text = model.Email;
        chIsAdmin.Checked = model.RoleID == eRole.Admin;
        txtPassword.ValidationSettings.RequiredField.IsRequired = false;
    }

    protected override void LoadModelDataFromControls(User model)
    {
        model.UserName = GetValue<string>(txtUserName);
        model.Email = GetValue<string>(txtEmail);
        model.DisplayName = GetValue<string>(txtDisplayName);
        model.RoleID = chIsAdmin.Checked ? eRole.Admin : eRole.User;
        if (!string.IsNullOrEmpty(GetValue<string>(txtPassword)))
        {
            model.Password = GetValue<string>(txtPassword);
        }
    }
}